package com.tbb.app.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.tbb.app.base.dto.UserCurUpdateDTO;
import com.tbb.app.base.dto.UserPasswordDTO;
import com.tbb.app.base.search.NoteUserPageSearch;
import com.tbb.app.base.service.*;
import com.tbb.app.base.vo.NoteVO;
import com.tbb.app.base.vo.UserVO;
import com.tbb.app.common.entity.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/api/phx")
@Slf4j
@ApiSupport(order = 5)
@Api(tags = "彭海鑫相关接口")
//@CrossOrigin(origins = "*")
public class PHXController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private INoteService iNoteService;
    @Autowired
    private IFollowService iFollowService;
    @Autowired
    private IFileService iFileService;
    @Autowired
    private IUserNoteService iUserNoteService;

    @GetMapping("/user/detail")
    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 1)
    public BaseResponse<UserVO> detailUser(
            @RequestParam(required = false)
            @ApiParam(value = "用户id，不填则表示本人")
                    String userId) {
        return BaseResponse.data(iUserService.detail(userId));
    }

    @PostMapping("/note/page")
    @ApiOperation(value = "根据用户id分页查询笔记")
    @ApiOperationSupport(order = 2)
    public BaseResponse<IPage<NoteVO>> pageNote(
            @RequestBody
            @Validated
                    NoteUserPageSearch search) {
        return BaseResponse.data(iNoteService.page(search));
    }

    @GetMapping("/user/follow")
    @ApiOperation(value = "关注用户")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Object> followUser(
            @RequestParam
            @ApiParam(value = "被关注用户id，必填", required = true)
                    String followedId,
            @RequestParam
            @ApiParam(value = "操作类型，必填，0-取消关注，1-关注", required = true)
                    int type) {
        iFollowService.follow(followedId, type);
        return BaseResponse.ok();
    }

    @GetMapping("/user/logout")
    @ApiOperation(value = "登出用户")
    @ApiOperationSupport(order = 4)
    public BaseResponse<Object> logout() {
        iUserService.logout();
        return BaseResponse.ok();
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "编辑当前用户")
    @ApiOperationSupport(order = 5)
    public BaseResponse<Object> updateUser(
            @RequestBody
            @Validated
                    UserCurUpdateDTO dto) {
        iUserService.update(dto);
        return BaseResponse.ok();
    }

    @PostMapping("/file/upload")
    @ApiOperation("文件上传")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParam(name = "file", value = "单文件上传", required = true, dataType = "MultipartFile", allowMultiple = true, paramType = "form")
    public BaseResponse<String> uploadFile(
            @RequestPart("file")
            @NotNull(message = "文件不能为空")
                    MultipartFile file) {
        return BaseResponse.data(iFileService.upload(file));
    }

    @PostMapping("/user/password/update")
    @ApiOperation(value = "修改密码")
    @ApiOperationSupport(order = 7)
    public BaseResponse<Object> updatePassword(
            @RequestBody
            @Validated
                    UserPasswordDTO dto) {
        iUserService.updatePassword(dto);
        return BaseResponse.ok();
    }

    @GetMapping("/note/like")
    @ApiOperation(value = "点赞笔记")
    @ApiOperationSupport(order = 8)
    public BaseResponse<Object> likeNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String noteId,
            @RequestParam
            @ApiParam(value = "操作类型，必填，0-取消点赞，1-点赞", required = true)
                    int type) {
        iUserNoteService.like(noteId, type);
        return BaseResponse.ok();
    }


}
